package com.mq.mqserver.datacore;

import com.mq.MqApplication;
import com.mq.mqserver.core.Binding;
import com.mq.mqserver.core.Exchange;
import com.mq.mqserver.core.ExchangeType;
import com.mq.mqserver.core.MSGQueue;
import com.mq.mqserver.mapper.MeatMapper;
import java.io.File;
import java.util.List;

/**
 * 通过这个类,整理数据库中的数据
 *
 */
public class DataBaseManage {
    //这里并不通过spring注入的方式管理,而是通过手动管理:
    private MeatMapper meatMapper;

    //对数据库进行初始化:
    public void init(){
        //手动管理对象
        meatMapper = MqApplication.context.getBean(MeatMapper.class);
        //检查数据库是否存在
        if (!checkDBExists()) {
            //当数据库不存在时,要先建库建表
            //先创建目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //建库建表
            createTable();
            //插入默认数据:主要就是交换机的默认交换机direct
            createDefaultExchange();
            System.out.println("[DataBaseManage] 数据库初始换完成!");
        }else{
            System.out.println("[DataBaseManage] 数据库已存在");
        }
    }

    //删除数据库
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean t = file.delete();
        if(t){
            System.out.println("[DataBaseManage] 删除数据库成功");
        }else{
            System.out.println("[DataBaseManage] 删除数据库失败");
        }
        //删除目录
        File file1 = new File("./data");
        //注意: windows上: 使用delete删除目录的时候,需要保证目录是空的,否则删除失败
        boolean d = file1.delete();
        if(d){
            System.out.println("[DataBaseManage] 删除目录成功");
        }else{
            System.out.println("[DataBaseManage] 删除目录失败");
        }
    }
    //检查数据库是否已经创建
    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        return file.exists();
    }
    //建表
    private void createTable() {
        meatMapper.createExchangeTable();
        meatMapper.createQueueTable();
        meatMapper.createBindingTable();
        System.out.println("[DataBaseManage] 建表完成!");
    }
    //初始化交换机:
    private void createDefaultExchange() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setAutoDelete(false);
        exchange.setDurable(false);
        meatMapper.insertExchange(exchange);
        System.out.println("[DataBaseManage] 初始换数据库完成!");
    }

    public void insertExchange(Exchange exchange){
        meatMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges(){
        return meatMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        meatMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        meatMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return meatMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName){
        meatMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        meatMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return meatMapper.selectAllBindings();
    }
    public void deleteBindings(String bindingKey){
        meatMapper.deleteBinding(bindingKey);
    }
}
